package leetcode.code2439;

public class Solution {
	public int minimizeArrayValue(int[] nums) {
		int l = 0, r = (int) 1e9;
		int ans = 0;
		while (l <= r) {
			int m = ((r - l) >> 1) + l;
			long over = 0;
			for (int i = nums.length - 1; i >= 0; i--) {
				if (over + nums[i] > m) {
					over = nums[i] + over - m;
				} else {
					over = 0;
				}
			}
			if (over == 0) {
				ans = m;
				r = m - 1;
			} else {
				l = m + 1;
			}
		}
		return ans;
	}
}
